package com.example.yichuangyun.ui.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.DataBindingHolder;
import com.example.yichuangyun.R;
import com.example.yichuangyun.databinding.ItemLabelBinding;
import com.example.yichuangyun.model.business.LabelModel;

@SuppressLint("NotifyDataSetChanged")
public class TimeAdapter extends BaseQuickAdapter<LabelModel, DataBindingHolder<ItemLabelBinding>> {

    private OnLabelClickListener onLabelClickListener;

    @Override
    protected void onBindViewHolder(@NonNull DataBindingHolder<ItemLabelBinding> itemLabelBindingDataBindingHolder, int i, @Nullable LabelModel labelModel) {
        ItemLabelBinding binding = itemLabelBindingDataBindingHolder.getBinding();
        if (null != labelModel) {
            binding.tvLabel.setText(labelModel.getRemark());
            if (labelModel.isSelect()) {
                binding.tvLabel.setTextColor(0xFF426FFF);
                binding.tvLabel.setBackgroundResource(R.drawable.shape_time_bg_sel);
            } else {
                binding.tvLabel.setTextColor(0xFF4A4A4A);
                binding.tvLabel.setBackgroundResource(R.drawable.shape_label_bg_def);
            }
            binding.tvLabel.setOnClickListener(v -> {
                if (labelModel.isSelect()) {
                    labelModel.setSelect(false);
                } else {
                    for (LabelModel model : getItems()) {
                        model.setSelect(false);
                    }
                    labelModel.setSelect(true);
                }
                notifyDataSetChanged();
                if (null != onLabelClickListener) {
                    onLabelClickListener.onLabelClick(labelModel);
                }
            });
        }
    }

    public void resetData() {
        for (LabelModel labelModel : getItems()) {
            labelModel.setSelect(false);
        }
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    protected DataBindingHolder<ItemLabelBinding> onCreateViewHolder(@NonNull Context context, @NonNull ViewGroup viewGroup, int i) {
        return new DataBindingHolder<>(R.layout.item_label, viewGroup);
    }

    public void setOnLabelClickListener(OnLabelClickListener onLabelClickListener) {
        this.onLabelClickListener = onLabelClickListener;
    }

    public interface OnLabelClickListener {
        void onLabelClick(LabelModel labelModel);
    }
}
